package cn.zch.soft.util.img2hex;
/***
 * @class ImgHandler 获得像素矩阵
 * @description TODO
 * @author zch
 * @date 2019/7/24
 * @version V0.0.1.201907241255.01
 * @function
 * @modfiyDate
 * @createDate
 * @package
 */
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.File;
import java.io.IOException;
/**
 * Created by Song on 2016/12/29.
 * 用于读取Image文件
 */
public final class ImgHandler {
	private static void getData(String path) {
		try {
			BufferedImage bimg = ImageIO.read(new File(path));
			int[][] data = new int[bimg.getWidth()][bimg.getHeight()];
			//方式一：通过getRGB()方式获得像素矩阵
			//此方式为沿Height方向扫描
			System.out.println("方式1:");
			for (int i = 0; i < bimg.getWidth(); i++) {
				for (int j = 0; j < bimg.getHeight(); j++) {
					data[i][j] = bimg.getRGB(i, j);
					//输出一列数据比对
					if (i == 0) {
						System.out.printf("%x-", data[i][j]);
					}
				}
			}
			//			Raster raster = bimg.getData();
			//			System.out.println("");
			//			int [] temp = new int[raster.getWidth()*raster.getHeight()*raster.getNumBands()];
			//			//方式二：通过getPixels()方式获得像素矩阵
			//			//此方式为沿Width方向扫描
			//			int [] pixels  = raster.getPixels(0,0,raster.getWidth(),raster.getHeight(),temp);
			//			for (int i=0;i<pixels.length;) {
			//				//输出一列数据比对
			//				if((i%raster.getWidth()*raster.getNumBands())==0)
			//					System.out.printf("ff%x%x%x\t",pixels[i],pixels[i+1],pixels[i+2]);
			//				i+=3;
			//			}
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	private static void getData2(String path) {
		try {
			//			BufferedImage bimg = ImageIO.read(new File(path));
			//			int[][] data = new int[bimg.getWidth()][bimg.getHeight()];
			//			//方式一：通过getRGB()方式获得像素矩阵
			//			//此方式为沿Height方向扫描
			//			for (int i = 0; i < bimg.getWidth(); i++) {
			//				for (int j = 0; j < bimg.getHeight(); j++) {
			//					data[i][j] = bimg.getRGB(i, j);
			//					//输出一列数据比对
			//					if (i == 0) {
			//						System.out.printf("%x\t", data[i][j]);
			//					}
			//				}
			//			}
			BufferedImage bimg = ImageIO.read(new File(path));
			Raster raster = bimg.getData();
			System.out.println("");
			int[] temp = new int[raster.getWidth() * raster.getHeight() * raster.getNumBands()];
			//方式二：通过getPixels()方式获得像素矩阵
			//此方式为沿Width方向扫描
			int[] pixels = raster.getPixels(0, 0, raster.getWidth(), raster.getHeight(), temp);
			System.out.println("方式2:");
			for (int i = 0; i < pixels.length; ) {
				//输出一列数据比对
				if ((i % raster.getWidth() * raster.getNumBands()) == 0)
					System.out.printf("ff%x%x%x-", pixels[i], pixels[i + 1], pixels[i + 2]);
				i += 3;
			}
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		getData("E:\\half-white-half-black(2).png");
		getData2("E:\\half-white-half-black(2).png");
	}
}
